package uk.ac.aber.Blockmation;

import java.awt.Color;
import java.awt.image.BufferedImage;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;


/**
 * Provides encode(...) method to converts ArrayList of BufferedImages -  a
 * movie/animation to a Scotty-compliant file type (text file).
 *
 * @author Aron
 */
public class ImagesIntoScotty
{
	/** Class translating colors into letters */
	private OurColors ourColors = new OurColors();
    /**
     * Encodes animation provided in BufferedImages into the Scotty text file type.
     * @param movie - ArrayList of BufferedImages that create movie
     * @param toFile - filename to save movie into
     *
     * @throws FileNotFoundException - if the file cannot be found
     * @throws IOException - if other I/O related problem occurs
     */
    public void encode(ArrayList<BufferedImage> movie, File toFile)
        throws FileNotFoundException, IOException
    {
        int noOfFrames = movie.size();

        // make sure there is at least one image/frame to save
        if (noOfFrames > 0)
        {
            PrintWriter out = new PrintWriter(new BufferedWriter(
                        new FileWriter(toFile)));
            
            /*
             * Scotty accepts only square frames.
             * The BufferedImage frame may not be square so
             * take the larger measure to set as frame size and 
             * you'll keep all the frame, with extra space filled
             * with background anyway. 
             */ 
            int frameWidth = movie.get(0).getWidth();
            int frameHeight = movie.get(0).getHeight();

            // if width != height take the max of them two
            int frameSize = Math.max(frameWidth, frameHeight);

            // write number of frames to the first line
            out.println(noOfFrames);
            
            //write frame size to the second line
            out.println(frameSize);

            // for keeping currently-processed frames in
            BufferedImage img;

            // the RGB color value as int to keep pixel color in
            int rgb;
            
            // the letter to be written out in a file representing color 
            char letter;

            for (int h = 0; h < noOfFrames; h++)
            {
            	// work on h-th frame from the movie at a time
                img = movie.get(h);
                
                for (int i = 0; i < frameSize; i++)
                {
                    for (int j = 0; j < frameSize; j++)
                    {
                    	//gets pixel at this position from img as RGB packed-int  
                        rgb = img.getRGB(j, i);
                        //translates the rgb color into letter
                        letter = ourColors.get(new Color(rgb));
                        //prints the letter into the line in file
                        out.print(letter);
                    }
                    // finished the line, next line (next row of pixels from frame)
                    out.println();
                }
            }
            // remember to close file stream
            out.close();
        }
        // Debugging info
        System.out.println(noOfFrames + " images converted to file: " + toFile);
    }
}
